package com.dataseek.iot.device.manager.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.base.IController;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.request.QueryPage;
import com.dataseek.iot.core.request.QueryPage2;
import com.dataseek.iot.core.request.QueryParam;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.core.util.ParseUtil;
import com.dataseek.iot.core.util.WrapperUtil;
import com.dataseek.iot.device.manager.handler.ProductFunctionHandler;
import com.dataseek.iot.mysql.entity.ProductFunction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cc
 * @since 2021-11-05
 */
@RestController
@RequestMapping("/product-function")
@Api(tags="产品功能属性接口")
public class ProductFunctionController implements IController<ProductFunction> {

    @Autowired
    ProductFunctionHandler functionService;


    @PutMapping("/addOne")
    @ApiOperation("保存单个产品功能属性")
    @Override
    public Mono<ResponseEntity> saveOne(@RequestBody ProductFunction entity) {
        ProductFunction user = entity;
        return functionService.saveOne(user);
    }

    @PostMapping("/addBatch")
    @ApiOperation("批量保存产品功能属性数据")
    @Override
    public Mono<ResponseEntity> saveBatch(@RequestBody List<ProductFunction> lists) {
        return functionService.saveBatch(lists);
    }


    @DeleteMapping("/{id}")
    @ApiOperation("根据ID移除产品功能属性信息")
    @Override
    public Mono<ResponseEntity> remove(@PathVariable String id) {
        return functionService.remove(id);
    }

    @DeleteMapping("/removeByIds")
    @ApiOperation("根据ID批量移除产品功能属性信息")
    @Override
    public Mono<ResponseEntity> removeAll(@RequestBody List<String> ids) {
        return functionService.removeAll(ids);
        //TODO 校验关联的其他信息
    }

    @DeleteMapping("/remove")
    @ApiOperation("根据条件移除产品功能属性信息")
    @Override
    public Mono<ResponseEntity> remove(@RequestBody List<QueryParam> params) {
        Wrapper wrapper = WrapperUtil.assembleParams(params);
        return functionService.remove(wrapper);
        //TODO 校验关联的其他信息
    }

    @PostMapping("/updateOne")
    @ApiOperation("更新产品功能属性信息")
    @Override
    public Mono<ResponseEntity> updateOne(@RequestBody ProductFunction entity) {
        return functionService.updateOne(entity);
    }

    @PostMapping("/update")
    @ApiOperation("根据条件更新产品功能属性信息")
    @Override
    public Mono<ResponseEntity> update(@RequestBody List<QueryParam> params) {
        Wrapper wrapper = WrapperUtil.assembleParams(params);
        return functionService.update(wrapper);
    }

    @PostMapping("/updatePassword")
    @ApiOperation("更新密码")
    public Mono<ResponseEntity> updatePassword(@RequestBody ProductFunction user){
        return functionService.updateOne(user);
    }

    @PostMapping("/updateBatch")
    @ApiOperation("批量更新")
    @Override
    public Mono<ResponseEntity> updateBatch(@RequestBody List<ProductFunction> entities) {
        return functionService.updateBatch(entities);
    }

    @PostMapping("/page/find")
    @ApiOperation("无条件分页查询")
//    @ApiImplicitParams(
//            @ApiImplicitParam(name = "page", value = "分页对象", required = true,
//                    example = "{" +
//                            " //当前页面，默认1"+
//                            "  current: 0," +
//                            " //是否命中count缓存 默认为 false"+
//                            "  hitCount: true," +
//                            "//单页分页条数限制"+
//                            "  maxLimit: 4," +
//                            "  //排序字段设置"+
//                            "  orders: [" +
//                            "    {asc: true, column: username }" +
//                            "  ]," +
//                            "  searchCount: true," +
//                            " //每页条数"+
//                            "  size: 10,"+
//                            "}")
//    )
    @Override
    public Mono<ResponseEntity> page(@RequestBody Page p) {
        return functionService.page(p);
    }

    @PostMapping("/page/findWithParam")
    @ApiOperation("分页查询，带查询条件")
    @Override
    public Mono<ResponseEntity> page(@RequestBody QueryPage queryPage) {
        if(queryPage==null||queryPage.getParams()==null||queryPage.getPage()==null){
            ResponseEntity entity  = new ResponseEntity();
            entity.setCode(ResponseConst.BAD_REQUEST_CODE);
            entity.setMsg(ResponseConst.BAD_REQUEST);
            return functionService.defaultResponseOne(entity);
        }
        return functionService.page(queryPage.getPage(), WrapperUtil.assembleParams(queryPage.getParams()));
    }

    /**
     * 根据IDS查询数据
     * @param ids
     * @return
     */
    @PostMapping("/findById")
    @ApiOperation("根据ID查询数据")
    @Override
    public Flux<ResponseEntity> findByIds(@RequestBody List<String> ids) {
        return functionService.find(ids);
    }

    /**
     * 根据查询条件查询数据
     * @param params
     * @return
     */
    @PostMapping("/findByParams")
    @ApiOperation("根据查询条件查询参数")
    @Override
    public Flux<ResponseEntity> find(@RequestBody List<QueryParam> params){
        ResponseEntity entity = new ResponseEntity();
        if(params==null){
            entity.setCode(ResponseConst.BAD_REQUEST_CODE);
            entity.setMsg(ResponseConst.BAD_REQUEST);
            return functionService.defaultResponseTwo(entity);
        }else if(params.size()==0){
            entity.setCode(ResponseConst.NULL_PARAM_CODE);
            entity.setMsg(ResponseConst.NULL_PARAM);
            return functionService.defaultResponseTwo(entity);
        }
        return functionService.find(WrapperUtil.assembleParams(params));
    }

    /**
     * 根据id条件查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询产品功能属性")
    @Override
    public Mono<ResponseEntity> findOne(@PathVariable String id) {
        return functionService.findOne(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询所有")
    @Override
    public Flux<ResponseEntity> findAll() {
        return functionService.findAll();
    }

    @PostMapping("/findByJSONParams")
    @ApiOperation("条件查询，没有分页")
    @Override
    public Flux<ResponseEntity> find(@RequestBody JSONObject json) {
        QueryWrapper wrapper = ParseUtil.parseJson(json);
        return functionService.find(wrapper);
    }

    @PostMapping("/page/findByJSONParams")
    @ApiOperation("分页查询，传参为json")
    @Override
    public Mono<ResponseEntity> page(@RequestBody JSONObject json) {
        QueryPage2 queryPage2 = ParseUtil.parseJsonWithPage(json);
        return functionService.page(queryPage2.getPage(),queryPage2.getWrapper());
    }
}
